package com.yyplatform.admin.platform.file.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyplatform.admin.infra.exception.ErrorCode;
import com.yyplatform.admin.infra.exception.ServiceException;
import com.yyplatform.admin.platform.config.component.SysConfigComponent;
import com.yyplatform.admin.platform.config.model.enums.ConfigKeyEnum;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigAliFormDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigFastDfsFormDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigFormDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigLocalFormDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigMinIOFormDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigQiniuFormDTO;
import com.yyplatform.admin.platform.file.model.dto.SysFileConfigTencentFormDTO;
import com.yyplatform.admin.platform.file.model.enums.FileStorageTypeEnum;
import com.yyplatform.admin.platform.file.service.FileConfigService;

import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2025-04-15
 */
@Service
@Slf4j
public class FileConfigServiceImpl implements FileConfigService {

    @Autowired
    private SysConfigComponent configComponent;

    @Value("${file.upload.path}")
    private String uploadFilePath;

    @Override
    public void saveConfig(SysFileConfigFormDTO sysFileConfigFormDTO) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String json = mapper.writeValueAsString(sysFileConfigFormDTO);
            configComponent.saveConfig(ConfigKeyEnum.FILE_UPLOAD_CONFIG.getValue(), json);
        } catch (JsonProcessingException e) {
            log.error("转换json异常", e);
            throw ServiceException.of(ErrorCode.UNKNOW_ERROR);
        }
    }


    private SysFileConfigLocalFormDTO getLocalConfig() {
        SysFileConfigLocalFormDTO localFormDTO = new SysFileConfigLocalFormDTO();

        localFormDTO.setRootPath(uploadFilePath);
        localFormDTO.setUrlPrefix("/files");
        localFormDTO.setDomain("http://localhost:8081");

        return localFormDTO;
    }


    @Override
    public SysFileConfigDTO getConfig() {
        String json = configComponent.getConfig(ConfigKeyEnum.FILE_UPLOAD_CONFIG.getValue(), null);
        SysFileConfigDTO configDTO = new SysFileConfigDTO();
        configDTO.setStorageType(FileStorageTypeEnum.LOCAL.getValue());


        if (StringUtils.isEmpty(json)) {
            configDTO.setLocalFormDTO(getLocalConfig());
            configDTO.setStorageType(FileStorageTypeEnum.LOCAL.getValue());


            configDTO.setAliFormDTO(new SysFileConfigAliFormDTO());
            configDTO.setTencentFormDTO(new SysFileConfigTencentFormDTO());
            configDTO.setQiniuFormDTO(new SysFileConfigQiniuFormDTO());
            configDTO.setMinIOFormDTO(new SysFileConfigMinIOFormDTO());
            configDTO.setFastDfsFormDTO(new SysFileConfigFastDfsFormDTO());

            return configDTO;
        }


        try {
            ObjectMapper mapper = new ObjectMapper();
            SysFileConfigDTO sysFileConfigDTO = mapper.readValue(json, SysFileConfigDTO.class);

            if (sysFileConfigDTO.getFastDfsFormDTO() == null) {
                configDTO.setFastDfsFormDTO(new SysFileConfigFastDfsFormDTO());
            }

            if (sysFileConfigDTO.getMinIOFormDTO() == null) {
                configDTO.setMinIOFormDTO(new SysFileConfigMinIOFormDTO());
            }

            if (sysFileConfigDTO.getAliFormDTO() == null) {
                configDTO.setAliFormDTO(new SysFileConfigAliFormDTO());
            }


            if (sysFileConfigDTO.getTencentFormDTO() == null) {
                configDTO.setTencentFormDTO(new SysFileConfigTencentFormDTO());
            }

            if (sysFileConfigDTO.getQiniuFormDTO() == null) {
                configDTO.setQiniuFormDTO(new SysFileConfigQiniuFormDTO());
            }

            return sysFileConfigDTO;
        } catch (JsonProcessingException e) {
            log.error("转换json异常", e);
            throw ServiceException.of(ErrorCode.UNKNOW_ERROR);
        }

    }

    @Override
    public String getUploadFilePath() {
        return uploadFilePath;
    }
}
